import Router from '@koa/router';
import { z } from 'zod';
import { validateBody, validateQuery } from '@middleware/validation.middleware';
import { createUser, deleteUser, getUser, getUsers, updateUser } from '@controllers/user.controller';

export const userRouter = new Router();

// Validation Schemas
const createUserSchema = z.object({
  email: z.string().email(),
  name: z.string().min(2).max(50).optional(),
});

const updateUserSchema = z.object({
  name: z.string().min(2).max(50).optional(),
  email: z.string().email().optional(),
}).refine((data) => Object.keys(data).length > 0, {
  message: "At least one field must be provided for update"
});

const userQuerySchema = z.object({
  page: z.string().regex(/^\d+$/).transform(Number).optional(),
  limit: z.string().regex(/^\d+$/).transform(Number).optional(),
});

// Routes
userRouter.get('/', validateQuery(userQuerySchema), getUsers);
userRouter.get('/:id', getUser);
userRouter.post('/', validateBody(createUserSchema), createUser);
userRouter.patch('/:id', validateBody(updateUserSchema), updateUser);
userRouter.delete('/:id', deleteUser);
